<template>
  <div>
    <!-- 面包导航 -->
    <el-breadcrumb separator="/" style="padding-left:10px;padding-bottom:10px;font-size:12px;">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-form :inline="true">
      <el-form-item>
        <el-button type="primary" icon="el-icon-plus" @click="dialogFormVisible = true" v-if="hasAuth('sys:menu:save')">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
        stripe
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column
          prop="name"
          label="名称"
          width="180">
      </el-table-column>

      <el-table-column
          prop="perm"
          label="权限编码"
          width="180">
      </el-table-column>

      <el-table-column
          prop="icon"
          label="图标">
      </el-table-column>

      <el-table-column
          prop="type"
          label="类型"
          width="120">
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.type === 0">目录</el-tag>
          <el-tag size="small" type="success" v-else-if="scope.row.type === 1">菜单</el-tag>
          <el-tag size="small" type="info" v-else-if="scope.row.type === 2">按钮</el-tag>
        </template>
      </el-table-column>

      <el-table-column
          prop="path"
          label="菜单URL">
      </el-table-column>

      <el-table-column
          prop="component"
          label="菜单组件">
      </el-table-column>

      <el-table-column
          prop="orderNum"
          label="排序号"
          width="70">
      </el-table-column>

      <el-table-column
          prop="status"
          label="状态"
          width="120">
        <template slot-scope="scope">
          <el-tag size="small" type="success" v-if="scope.row.status === 1">正常</el-tag>
          <el-tag size="small" type="danger" v-else-if="scope.row.status === 0">禁用</el-tag>
        </template>
      </el-table-column>

      <el-table-column
          label="操作"
          width="160">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="editHandle(scope.row.id)" v-if="hasAuth('sys:menu:update')">编辑</el-button>
          <el-divider direction="vertical"></el-divider>
          <template>
            <el-popconfirm
                title="确定要删除该菜单吗？"
                icon="el-icon-info"
                icon-color="red"
                @confirm="delHandle(scope.row.id)"
            >
              <el-button size="mini" type="danger" slot="reference" v-if="hasAuth('sys:menu:delete')">删除</el-button>
            </el-popconfirm>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="新增菜单" :visible.sync="dialogFormVisible" width="600px" :before-close="handleClose">
      <el-form :model="editForm" :rules="editFormRules" ref="editForm">
        <el-form-item label="上级菜单" prop="parentId" label-width="100px">
          <el-popover
              placement="top-start"
              title="提示"
              width="200"
              trigger="hover"
              content="当上级菜单为0时表示最顶级根目录，系统管理的父菜单就为0">

            <!--模拟树形下拉框-->
            <el-select v-model="editForm.parentId" slot="reference" placeholder="请选择上级菜单">
              <template v-for="item in tableData">
                <el-option :label="item.name" :value="item.id" ></el-option>
                <template v-for="child in item.children">
                  <el-option :label="child.name" :value="child.id">
                    <span>{{ '- ' + child.name }}</span>
                  </el-option>
                </template>
              </template>
            </el-select>
          </el-popover>
        </el-form-item>
        <el-form-item label="菜单名称" prop="name" label-width="100px">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限编码" prop="perms" label-width="100px">
          <el-input v-model="editForm.perms" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="icon" label-width="100px">
          <el-input v-model="editForm.icon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单URL" prop="path" label-width="100px">
          <el-input v-model="editForm.path" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单组件" prop="component" label-width="100px">
          <el-input v-model="editForm.component" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type" label-width="100px">
          <el-radio-group v-model="editForm.type">
            <el-radio :label=0>目录</el-radio>
            <el-radio :label=1>菜单</el-radio>
            <el-radio :label=2>按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="status" label-width="100px">
          <el-radio-group v-model="editForm.status">
            <el-radio :label=0>禁用</el-radio>
            <el-radio :label=1>正常</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序号" prop="orderNum" label-width="100px">
          <el-input-number v-model="editForm.orderNum" :min="1" label="排序号">1</el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('editForm')">提交</el-button>
        <el-button @click="reset('editForm')">重置</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "Menu",
  data() {
    return {
      dialogFormVisible: false, // 弹框显示
      tableData: [], // 数据
      editForm: {parentId: 0}, // 编辑对话框信息
      editFormRules: {
        parentId: [
          {required: true, message: '请选择上级菜单', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入名称', trigger: 'blur'}
        ],
        perms: [
          {required: true, message: '请输入权限编码', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '请选择类型', trigger: 'change'}
        ],
        orderNum: [
          {required: true, message: '请填入排序号', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '请选择状态', trigger: 'change'}
        ]
      }
    }
  },
  created() {
    this.getMenuTree()
  },
  methods: {
    //关闭表单，对表单进行重置
    handleClose(){
      this.resetForm('editForm')
    },
    //删除菜单项
    delHandle(id) {
      request.post("/sys/menu/delete/" + id).then(res => {
        this.$message({
          showClose: true,
          message: '恭喜你，删除菜单成功',
          type: 'success',
          onClose: () => {
            this.getMenuTree()
          }
        });
      })
    },
    //获取编辑菜单项
    editHandle(id) {
      request.get("/sys/menu/info/" + id).then(res => {
        //数据回显
        this.editForm = res.data
        //打开对话框
        this.dialogFormVisible = true
      })
    },
    //重置表单
    reset(formName) {
      this.$refs[formName].resetFields()
      this.editForm = {}
      this.editForm.parentId = 0
    },
    //清空表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.editForm = {}
      this.dialogFormVisible = false
    },
    //获取树形菜单
    getMenuTree() {
      request.get("/sys/menu/list").then(res => {
        this.tableData = res.data
      })
    },
    //提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 有id就是修改，否则就是新增
          request.post('/sys/menu/' + (this.editForm.id ? "update" : "save"), this.editForm)
              .then(res => {
                console.log(res.data)
                this.resetForm(formName)
                //提示信息
                this.$message({
                  showClose: true,
                  message: '恭喜你，编辑菜单成功',
                  type: 'success',
                  //关闭这条消息时更新数据
                  onClose: () => {
                    this.getMenuTree()
                  }
                });
                this.resetForm('editForm')
                //关闭对话框
                this.dialogFormVisible = false
              })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>

</style>